package com.mju.javaee_1;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectServiceImpl {
    public void sayHello(String name){
        System.err.println("Hello "+name);
    }
    public ReflectServiceImpl getInstance()
    {
        ReflectServiceImpl object = null;
        try {
            object = (ReflectServiceImpl)Class.forName("javaee.ReflectServiceImpl").newInstance();
        }
        catch(ClassNotFoundException|InstantiationException|IllegalAccessException ex){
            ex.printStackTrace();
        }
        return object;
    }
    public Object reflectMethod(){
        Object returnObj = null;
        ReflectServiceImpl target = new ReflectServiceImpl();
        try{
            Method method = ReflectServiceImpl.class.getMethod("sayHello", String.class);
            returnObj = method.invoke(target, "");
        }catch(NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex){
            ex.printStackTrace();
        }
        return returnObj;
    }
    public Object reflect(){
        ReflectServiceImpl object = null;
        try{
            object = (ReflectServiceImpl)Class.forName("javaee.ReflectServiceImpl").newInstance();
            Method method = ReflectServiceImpl.class.getMethod("sayHello", String.class);
            method.invoke(object, "");
        }
        catch(ClassNotFoundException | InstantiationException | IllegalAccessException | NoSuchMethodException | SecurityException | IllegalArgumentException | InvocationTargetException ex){
            ex.printStackTrace();
        }
        return object;
    }

}
